from django.http import HttpResponse
from django.shortcuts import render
import random
from utils.tencent.sms import send_sms_single
from django.conf import settings


# Create your views here.


def send_sms(request):
    tpl = request.GET.get('tpl')
    template_id = settings.TENCENT_SMS_TEMPLATE.get(tpl)
    if not template_id:
        return HttpResponse('模版不存在')
    code = random.randrange(1000, 9999)
    if tpl == 'register':
        res = send_sms_single('+8618516142269', template_id, [code, 5])
    elif tpl == 'login':
        res = send_sms_single('+8618516142269', template_id, [code,])
        print(res)
    if res['result'] == 0:
        return HttpResponse('success')
    else:
        return HttpResponse(res['errmsg'])
